package com.focusai.efairy.business.dev;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.focusai.efairy.R;

/**
 * 通知栏显示更新包下载进度的通知
 *
 */

public class DeviceMsgNotification {

    private static final int DOWNLOAD_TAG = 1111;

    private static DeviceMsgNotification mInstance;

    private Context mContext;

    private NotificationManager mNotificationManager;
    private Notification mNotification;
    private NotificationCompat.Builder mBuilder;
    private RemoteViews mRemoteViews;

    private DeviceMsgNotification(Context context){
        this.mContext = context;
    }

    public static DeviceMsgNotification getInstance(Context context){
        if (mInstance == null){
            synchronized (DeviceMsgNotification.class){
                if (mInstance == null){
                    mInstance = new DeviceMsgNotification(context);
                }
            }
        }

        return mInstance;
    }

    private void initDownloadProgress() {
        mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.view_device_notification);
        mBuilder = new NotificationCompat.Builder(mContext);
        //Ticker是状态栏显示的提示
        mBuilder.setTicker(mContext.getResources().getString(R.string.app_name));

        mBuilder.setSmallIcon(R.drawable.app_icon)
                .setContentTitle("设备消息...")
                .setWhen(System.currentTimeMillis())
                .setContent(mRemoteViews);

        mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
        mNotification = mBuilder.build();

        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(DOWNLOAD_TAG, mNotification);
    }

    public void updateMessage(String title,String msg) {
        if (null == mRemoteViews) {
            initDownloadProgress();
        }
        mRemoteViews.setTextViewText(R.id.tv_msg_title,title );
        mRemoteViews.setTextViewText(R.id.tv_msg_content,msg );
        mNotificationManager.notify(DOWNLOAD_TAG, mNotification);
    }

    public void cancelNotification() {
        if (null != mNotification) {
            mNotificationManager.cancel(DOWNLOAD_TAG);
        }
    }

}
